﻿using System;
using System.Collections.Generic;

using System.Xml.Linq;
using System.Globalization;

namespace PIT.Labs.ProjectRepository.Owl2DotNet.Rdf
{
    /// <summary>
    /// Entity base class for entities from RDF/OWL XML ontologies.
    /// </summary>
    public abstract class RdfEntityBase : IEntity
    {
        internal Dictionary<IAnnotationProperty, IValue> annotations;

        /// <summary>
        /// Create a new entity from an RDF/OWL XML node and set the containing ontology.
        /// </summary>
        /// <param name="node">xml node to create the entity from</param>
        /// <param name="o">ontology that defines the entity</param>
        protected RdfEntityBase(XElement node, RdfOntology o) : this(o)
        {
            Init(node);
        }

        /// <summary>
        /// Initialize the entity from an RDF/OWL XML node.
        /// </summary>
        /// <param name="node">xml node to create the entity from</param>
        protected RdfEntityBase(XElement node)
        {
            Init(node);
        }

        /// <summary>
        /// Create an empty entity and set the containing ontology
        /// </summary>
        /// <param name="o">ontology that defines the entity</param>
        protected RdfEntityBase(RdfOntology o)
        {
            this.RdfOntology = o;
        }

        private void Init(XElement node){
            this.Node = node;
            this.Label = RdfXmlParser.GetLabel(node);

            this.Uri = new Uri(RdfXmlParser.GetElementUri(node));
        }

        /// <summary>
        /// Get the XML node that this entity was created from
        /// </summary>
        public XElement Node
        {
            get;
            set;
        }

        /// <summary>
        /// Get the URI that identifies this entity in the ontology
        /// </summary>
        public Uri Uri
        {
            get;
            set;
        }

        /// <summary>
        /// Get the human readable label of the entity
        /// </summary>
        public string Label
        {
            get;
            set;
        }

        /// <summary>
        /// Get the RDF ontology that this entity belongs to
        /// <seealso cref="RdfEntityBase.Ontology"/>
        /// </summary>
        public RdfOntology RdfOntology
        {
            get;
            set;
        }

        /// <summary>
        /// Get the ontology that this entity belongs to
        /// </summary>
        public IOntology Ontology
        {
            get
            {
                return RdfOntology;
            }
        }

        /// <summary>
        /// Accessor for the annotation for this entity
        /// </summary>
        public IDictionary<IAnnotationProperty, IValue> Annotations
        {
            get
            {
                if (annotations == null)
                {
                    annotations = new Dictionary<IAnnotationProperty, IValue>();

                    RdfXmlParser.GetAnnotationProperties(this);
                }
                return annotations;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return String.Format( CultureInfo.InvariantCulture, "{0}: {1} ({2})", this.GetType().Name, this.Label, this.Uri);
        }
    }
}
